package master;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class RechnerButtonLeiste extends JPanel {
	private static final long serialVersionUID = 1L;
	private Rechner rechner;
	private RechnerButton startButton;
	private RechnerButton stopButton;
	private RechnerButton resumeButton;

	public RechnerButtonLeiste() {

	}

	public RechnerButtonLeiste(Rechner rechner) {
		this.setRechner(rechner);
		this.setFont(new Font("Arial",0,20));
		this.setResumeButton(new RechnerButton("Weiter", rechner));
		this.getResumeButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getRechner().resume();
				getStopButton().setText("Stop");
				getStopButton().setBackground(null);
				start();
			}
		});

		this.setStartButton(new RechnerButton("Start", rechner));
		this.getStartButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getRechner().start();
				getStopButton().setText("Stop");
				getStopButton().setBackground(null);
				start();
			}
		});

		this.setStopButton(new RechnerButton("Stop", rechner));
		this.getStopButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println(getStopButton().getText() + " "
						+ getRechner().getName());
				getRechner().stop();
				System.out.println("---> " + getRechner().getTime());
				getStopButton().setText("ABRECHNUNG");
				getStopButton().setBackground(Color.MAGENTA);
				stop();
			}
		});
		// this.getStopButton().setEnabled(false);
		this.setLayout(new GridLayout(1, 1));
		this.add(getStartButton());
		this.getResumeButton().setFont(getFont());
		this.getStartButton().setFont(getFont());
		this.getStopButton().setFont(getFont());
		this.setPreferredSize(new Dimension(100, 100));
		this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
		this.setOpaque(true);
	}

	public AbstractButton getStopButton() {
		return this.stopButton;
	}

	public void setStopButton(RechnerButton rechnerButton) {
		this.stopButton = rechnerButton;
	}

	public Rechner getRechner() {
		return rechner;
	}

	public void setRechner(Rechner rechner) {
		this.rechner = rechner;
	}

	public RechnerButton getStartButton() {
		return startButton;
	}

	public void setStartButton(RechnerButton startButton) {
		this.startButton = startButton;
	}

	public RechnerButton getResumeButton() {
		return resumeButton;
	}

	public void setResumeButton(RechnerButton startButton) {
		this.resumeButton = startButton;
	}

	public void start() {
		this.removeAll();
		this.add(getStopButton());
		if (getRechner().getMain().getServer()!=null)
			try {
				getRechner().getMain().getServer().setTaken(getRechner().getNumber(),true);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
	}

	public void stop() {
		this.removeAll();
		this.setLayout(new GridLayout(1, 2));
		this.add(getStopButton());
		this.add(getResumeButton());
	}

	public void reset() {
		getStopButton().setBackground(null);
		getStopButton().setText("Stop");
		try {
			getRechner().getMain().getServer().setTaken(getRechner().getNumber(),false);
		} catch (RemoteException e) {			
			e.printStackTrace();
		}
		this.removeAll();
		
		this.add(getStartButton());
	}

}
